package com.enhe.gck.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.enums.WeighingResultEnum;
import com.enhe.gck.exception.BusinessException;
import com.enhe.gck.exception.WeighException;
import com.enhe.gck.item.BatchHWANE20386Item;
import com.enhe.gck.pojo.Batch;
import com.kuangkie.carbon.common.IntegrationMsg;
import com.kuangkie.carbon.enun.SortDirection;
import com.kuangkie.carbon.panel.CarbonPanel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.enhe.gck.constant.FrontPrompt.Batch_Update_Cement_Fail;

@Service
@Slf4j
public class BatchService {

    /**
     * 获取启用&有剩余量&类型&批次类型匹配的批次列表
     */
    public List<Batch> getAvailableBatchList(String type, String batchType) {
        List<Batch> batchList = CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.出场批次_ccpc).getProConJunctionFactory()
                .addEqual(BatchHWANE20386Item.基本属性组_启用, true)
                .addNotEqual(BatchHWANE20386Item.基本属性组_剩余数量, 0.0)
                .addEqual(BatchHWANE20386Item.基本属性组_类型, type)
                .addEqual(BatchHWANE20386Item.基本属性组_批次类型, batchType)
                .addEqual(BatchHWANE20386Item.基本属性组_删除, false)
                .addIsNotNull(BatchHWANE20386Item.基本属性组_包装日期) // 包装日期不为空
                .goBack()
                .addSortItem(BatchHWANE20386Item.基本属性组_签发日期, SortDirection.ASC)
                .addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.出场批次_ccpc), SortDirection.ASC)
                .queryBeans(Batch.class);
        if (CollectionUtil.isEmpty(batchList)) {
            return Collections.emptyList();
        }
        return batchList;
    }

    /**
     * 获取启用&有剩余量&没有包装日期的批次列表
     */
    public List<Batch> getAvailableBatchListWithoutPackageDate(String type, String batchType) {
        List<Batch> batchList = CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.出场批次_ccpc).getProConJunctionFactory()
                .addEqual(BatchHWANE20386Item.基本属性组_启用, true)
                .addNotEqual(BatchHWANE20386Item.基本属性组_剩余数量, 0.0)
                .addEqual(BatchHWANE20386Item.基本属性组_类型, type)
                .addEqual(BatchHWANE20386Item.基本属性组_批次类型, batchType)
                .addIsNull(BatchHWANE20386Item.基本属性组_包装日期)
                .addEqual(BatchHWANE20386Item.基本属性组_删除, false)
                .goBack()
                .addSortItem(BatchHWANE20386Item.基本属性组_签发日期, SortDirection.ASC)
                .addSortItem(PublicMethod.createTimeStr(CarbonBaseConstant.出场批次_ccpc), SortDirection.ASC)
                .queryBeans(Batch.class);
        if (CollectionUtil.isEmpty(batchList)) {
            return Collections.emptyList();
        }
        return batchList;
    }

    public void updateSumResidueBatch(List<Batch> updateBatchList) {
        String batchNumberStr = updateBatchList.stream().map(Batch::getNumber).collect(Collectors.joining(","));
        Collection<IntegrationMsg> msgCollection = CarbonPanel.getRecordIntegration(CarbonBaseConstant.出场批次_ccpc)
                .integrate(updateBatchList);
        if (msgCollection.stream().anyMatch(e -> !e.success())) {
            String errMsg = msgCollection.stream().filter(e -> !e.success()).map(IntegrationMsg::getRefuseMsgContentStr).collect(Collectors.joining(","));
            log.error("更新批次【{}】剩余量失败!原因:{}", batchNumberStr, errMsg);
            throw new BusinessException(Batch_Update_Cement_Fail);
        } else {
            log.warn("更新批次【{}】剩余量成功!", batchNumberStr);
        }
    }

    public void updatePackageDateBatch(List<Batch> toUpdateBatchList) {
        String batchNumberStr = toUpdateBatchList.stream().map(Batch::getNumber).collect(Collectors.joining(","));
        Collection<IntegrationMsg> msgCollection = CarbonPanel.getRecordIntegration(CarbonBaseConstant.出场批次_ccpc)
                .integrate(toUpdateBatchList);
        if (msgCollection.stream().anyMatch(e -> !e.success())) {
            String errMsg = msgCollection.stream().filter(e -> !e.success()).map(IntegrationMsg::getRefuseMsgContentStr).collect(Collectors.joining(","));
            log.error("更新批次【{}】打包日期失败！原因:{}", batchNumberStr, errMsg);
            throw new WeighException(WeighingResultEnum.System_Abnormal, Batch_Update_Cement_Fail);
        } else {
            log.warn("更新批次打包日期成功!【{}】", batchNumberStr);
        }
    }

    public Batch getByName(String batchNumber) {
        return CarbonPanel.getRecordQueryFactory(CarbonBaseConstant.出场批次_ccpc).getProConJunctionFactory()
                .addEqual(BatchHWANE20386Item.基本属性组_水泥编号, batchNumber)
                .goBack().queryABean(Batch.class);
    }
}
